﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace HYView
{
	public partial class Partial3DViewForm : System.Windows.Forms.Form
	{
		public Partial3DViewForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.m_Doc = this.m_Revit.Application.ActiveUIDocument.Document;
			this.InitializeComponent();
		}

		public bool ShowSectionBox
		{
			get
			{
				return this.chbShowSectionBox.Checked;
			}
		}

		public bool Show3DGird
		{
			get
			{
				return this.chbShow3DGird.Checked;
			}
		}

		public bool Temp3dViewNameSetting
		{
			get
			{
				return this.chbTemp3dViewNameSetting.Checked;
			}
		}

		public ViewDetailLevel DetailLevel
		{
			get
			{
                ViewDetailLevel result = (ViewDetailLevel)1;
				if (this.cmbDetailLevel.SelectedIndex == 1)
				{
                    result = (ViewDetailLevel)2;
				}
				else if (this.cmbDetailLevel.SelectedIndex == 2)
				{
                    result = (ViewDetailLevel)3;
				}
				return result;
			}
		}

		public DisplayStyle ViewDisplayStyle
		{
			get
			{
                DisplayStyle result = (DisplayStyle)1;
                switch ((int)this.cmbDisplayStyle.SelectedIndex)
				{
				case 0:
                result = (DisplayStyle)1;
					break;
				case 1:
                    result = (DisplayStyle)2;
					break;
				case 2:
                    result = (DisplayStyle)4;
					break;
				case 3:
                    result = (DisplayStyle)7;
					break;
				case 4:
                    result = (DisplayStyle)6;
					break;
				}
				return result;
			}
		}

		public double TopElevation()
		{
			Level level;
			if (this.rbCurrentFloor.Checked)
			{
				ViewPlan viewPlan = this.m_Revit.Application.ActiveUIDocument.ActiveView as ViewPlan;
				level = this.GetNextLevel(viewPlan.GenLevel, false);
			}
			else if (this.rbSelectOneFloor.Checked)
			{
				Level levelCur = this.cmbBoxSelFloor1.SelectedItem as Level;
				level = this.GetNextLevel(levelCur, false);
			}
			else
			{
				level = (this.cmbBoxSelFloor1.SelectedItem as Level);
			}
			double elevation = level.Elevation;
			int num = Convert.ToInt32(this.tbTopOffset.Text);
			return elevation + AssistFunc.mmToFeet((double)num);
		}

		public double BottomElevation()
		{
			Level level;
			if (this.rbCurrentFloor.Checked)
			{
				level = (this.m_Revit.Application.ActiveUIDocument.ActiveView as ViewPlan).GenLevel;
			}
			else if (this.rbSelectOneFloor.Checked)
			{
				level = (this.cmbBoxSelFloor1.SelectedItem as Level);
			}
			else
			{
				level = (this.cmbBoxSelFloor2.SelectedItem as Level);
			}
			double elevation = level.Elevation;
			int num = Convert.ToInt32(this.tbBottomOffset.Text);
			return elevation + AssistFunc.mmToFeet((double)num);
		}

		private Level GetNextLevel(Level levelCur, bool bLowLevel = true)
		{
			try
			{
				int num = this.m_lstLevel.FindIndex((Level level) => level.Name == levelCur.Name);
				if (bLowLevel && num > 0)
				{
					return this.m_lstLevel[num - 1];
				}
				if (!bLowLevel && num < this.m_lstLevel.Count - 1)
				{
					return this.m_lstLevel[num + 1];
				}
			}
			catch
			{
			}
			return levelCur;
		}

		private void Partial3DViewForm_Load(object sender, EventArgs e)
		{
			this.m_lstLevel = this.GetSortLevels(this.m_Doc);
			this.cmbBoxSelFloor1.DisplayMember = "Name";
			this.cmbBoxSelFloor1.Items.AddRange(this.m_lstLevel.ToArray());
			this.cmbBoxSelFloor2.DisplayMember = "Name";
			this.cmbBoxSelFloor2.Items.AddRange(this.m_lstLevel.ToArray());
			this.LoadStoreFormData();
		}

		private void Partial3DViewForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK && this.rbSelectMultiFloors.Checked)
			{
				if (this.cmbBoxSelFloor1.SelectedIndex < this.cmbBoxSelFloor2.SelectedIndex)
				{
					YJKMessageBox.Show("选择的顶部楼层低于底部楼层。");
					e.Cancel = true;
					return;
				}
				if (this.Lessthan_Or_Equal(this.TopElevation(), this.BottomElevation()))
				{
					YJKMessageBox.Show("顶标高不能小于或者等于底标高。");
					e.Cancel = true;
					return;
				}
			}
			this.WriteStoreFormDate();
		}

		private bool Lessthan_Or_Equal(double val1, double val2)
		{
			return val1 - val2 <= 1E-09;
		}

		private void tbTopOffset_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int.Parse(this.tbTopOffset.Text);
			}
			catch (FormatException)
			{
				YJKMessageBox.Show("请输入一个整数。");
				e.Cancel = true;
			}
		}

		private void tbBottomOffset_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int.Parse(this.tbBottomOffset.Text);
			}
			catch (FormatException)
			{
				YJKMessageBox.Show("请输入一个整数。");
				e.Cancel = true;
			}
		}

		private List<Level> GetSortLevels(Autodesk.Revit.DB.Document doc)
		{
			List<Level> list = new FilteredElementCollector(doc).OfClass(typeof(Level)).Cast<Level>().ToList<Level>();
			list.Sort(new Partial3DViewForm.LevelComparer());
			return list;
		}

		private void LoadStoreFormData()
		{
			string path = Path.GetTempPath() + "Partial3DViewForm.dat";
			if (!File.Exists(path))
			{
				this.rbCurrentFloor.Checked = true;
				this.rbSelectOneFloor.Checked = false;
				this.rbSelectMultiFloors.Checked = false;
				this.lbSelFloor1.Text = "当前楼层:";
				this.SetCmbboxSelFloor1SelectIndex();
				this.lbSelFloor2.Visible = false;
				this.cmbBoxSelFloor2.Visible = false;
				this.tbTopOffset.Text = "0";
				this.tbBottomOffset.Text = "0";
				this.chbShowSectionBox.Checked = false;
				this.chbTemp3dViewNameSetting.Checked = true;
				this.chbShow3DGird.Checked = false;
				this.cmbDetailLevel.SelectedIndex = 0;
				this.cmbDisplayStyle.SelectedIndex = 0;
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.rbCurrentFloor.Checked = Convert.ToBoolean(streamReader.ReadLine());
			if (this.rbCurrentFloor.Checked)
			{
				this.SetSelectCurrentFloorMode();
			}
			this.rbSelectOneFloor.Checked = Convert.ToBoolean(streamReader.ReadLine());
			if (this.rbSelectOneFloor.Checked)
			{
				this.SetSelectOneFloorMode();
			}
			this.rbSelectMultiFloors.Checked = Convert.ToBoolean(streamReader.ReadLine());
			if (this.rbSelectMultiFloors.Checked)
			{
				this.SetSelectMultiFloorsMode();
			}
			this.chbShowSectionBox.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.chbTemp3dViewNameSetting.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.tbTopOffset.Text = streamReader.ReadLine();
			this.tbBottomOffset.Text = streamReader.ReadLine();
			this.cmbDetailLevel.SelectedIndex = Convert.ToInt32(streamReader.ReadLine());
			this.cmbDisplayStyle.SelectedIndex = Convert.ToInt32(streamReader.ReadLine());
			try
			{
				this.chbShow3DGird.Checked = Convert.ToBoolean(streamReader.ReadLine());
			}
			catch
			{
				streamReader.Close();
				fileStream.Close();
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SetCmbboxSelFloor1SelectIndex()
		{
			ViewPlan viewPlan = this.m_Revit.Application.ActiveUIDocument.ActiveView as ViewPlan;
			int num = 0;
			IEnumerator enumerator = this.cmbBoxSelFloor1.Items.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if ((enumerator.Current as Level).Name == viewPlan.GenLevel.Name)
					{
						this.cmbBoxSelFloor1.SelectedIndex = num;
						break;
					}
					num++;
				}
			}
		}

		private void WriteStoreFormDate()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "Partial3DViewForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.rbCurrentFloor.Checked);
			streamWriter.WriteLine(this.rbSelectOneFloor.Checked);
			streamWriter.WriteLine(this.rbSelectMultiFloors.Checked);
			streamWriter.WriteLine(this.chbShowSectionBox.Checked);
			streamWriter.WriteLine(this.chbTemp3dViewNameSetting.Checked);
			streamWriter.WriteLine(this.tbTopOffset.Text);
			streamWriter.WriteLine(this.tbBottomOffset.Text);
			streamWriter.WriteLine(this.cmbDetailLevel.SelectedIndex);
			streamWriter.WriteLine(this.cmbDisplayStyle.SelectedIndex);
			streamWriter.WriteLine(this.chbShow3DGird.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void rbCurrentFloor_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbCurrentFloor.Checked)
			{
				this.SetSelectCurrentFloorMode();
			}
		}

		private void rbSelectOneFloor_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbSelectOneFloor.Checked)
			{
				this.SetSelectOneFloorMode();
			}
		}

		private void rbSelectMultiFloors_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbSelectMultiFloors.Checked)
			{
				this.SetSelectMultiFloorsMode();
			}
		}

		private void SetSelectCurrentFloorMode()
		{
			this.rbSelectOneFloor.Checked = false;
			this.rbSelectMultiFloors.Checked = false;
			this.lbSelFloor1.Text = "当前楼层:";
			this.SetCmbboxSelFloor1SelectIndex();
			this.cmbBoxSelFloor1.Enabled = false;
			this.lbSelFloor2.Visible = false;
			this.cmbBoxSelFloor2.Visible = false;
		}

		private void SetSelectOneFloorMode()
		{
			this.rbCurrentFloor.Checked = false;
			this.rbSelectMultiFloors.Checked = false;
			this.lbSelFloor1.Text = "选择楼层:";
			this.cmbBoxSelFloor1.Enabled = true;
			this.SetCmbboxSelFloor1SelectIndex();
			this.lbSelFloor2.Visible = false;
			this.cmbBoxSelFloor2.Visible = false;
		}

		private void SetSelectMultiFloorsMode()
		{
			this.rbCurrentFloor.Checked = false;
			this.rbSelectOneFloor.Checked = false;
			this.lbSelFloor1.Text = "顶部楼层";
			this.cmbBoxSelFloor1.Enabled = true;
			this.cmbBoxSelFloor1.SelectedIndex = this.cmbBoxSelFloor2.Items.Count - 1;
			this.lbSelFloor2.Visible = true;
			this.cmbBoxSelFloor2.Visible = true;
			this.lbSelFloor2.Text = "底部楼层:";
			this.cmbBoxSelFloor2.SelectedIndex = 0;
		}

		private ExternalCommandData m_Revit;

		private Document m_Doc;

		private List<Level> m_lstLevel = new List<Level>();

		private class LevelComparer : IComparer<Level>
		{
			public int Compare(Level x, Level y)
			{
				return x.Elevation.CompareTo(y.Elevation);
			}
		}
	}
}
